var apireadyMap = [memberUinfo, initPage];

function initPage() {
    $('.container').on('click', '.contbox a', function(e) {
        var e = e || window.event;
        e.preventDefault();
        $(this).addClass('active').parent().siblings().find('a').removeClass('active');
    });

    // 续费
    $('.btnSubmit').on('click', submitDatabase);
}

// 检测是否下单成功
function detection() {
    // 获取订单号
    var order = tool.getSearch('order');
    if(order) {
        var ele = '';
        ele += '<div class="text-center">';
        ele += '<img src="../../../../public/index/image/loading.gif"/>';
        ele += '<p class="p-t-1x">正在确认订单，请稍后</p>';
        ele += '</div>';
        layer.open({
            content: ele
        });
        getOrder(order);
    }
}

var orderTimeout = null;
var orderagin = 1;

// 获取订单
function getOrder(order) {
    ajax.get('/api?order.query', {
        out_trade_no: order
    }, function(res) {
        if(res.state) {
            if(res.data == true) {
                layer.closeAll();
                document.querySelector('.action-close').click();
            } else {
                if (orderagin <= 2) {
                    ++orderagin;
                    orderTimeout = setTimeout(function() {
                        getOrder(order);
                    }, 1250);
                } else {
                    orderagin = 1;
                    layer.closeAll();
                }
            }
        } else {
            if (orderagin <= 2) {
                ++orderagin;
                orderTimeout = setTimeout(function() {
                    getOrder(order);
                }, 1250);
            } else {
                orderagin = 1;
                layer.closeAll();
            }
        }
    });
}

//渲染用户信息
function memberUinfo() {
	var modalId = modal.load.show();
    ajax.get('/api?member.uinfo', function(res) {
        modal.load.hide(modalId);
        if(res.state) {
            initMemberItem();
            var list = res.data;
            $("#ipt-nickname").html(list.nickname);
            $("#ipt-mobile").html(list.mobile);
            $("#ipt-expire").html(tool.formatDate("yy-mm-dd", list.expire));
            $("#ipt-headimgurl").attr("src", list.headimgurl);
        }
    });
}

// 加载选项
function initMemberItem() {
    var modalId = modal.load.show();
    ajax.get('/api?pay.item', function(res) {
        modal.load.hide(modalId);
        if(res.state) {
            createMemberItem(res.data);
        }
        // 查单
        detection();
    });
}

function createMemberItem(data) {
    var ele = '';
    var _idx = 1;
    for(var i in data) {
        var list = data[i];
        ele += '<li class="contbox">';
        if(_idx == 1) {
            ele += '<a href="javascript:;" class="active" data-key="' + i + '">';
            ++_idx;
        } else {
            ele += '<a href="javascript:;" class="" data-key="' + i + '">';
        }
        ele += '<p>' + i + '天</p>';
        ele += '<font class="price">' + list + '元</font>';
        ele += '</a>';
        ele += '</li>';
    }
    document.querySelector('#databaseVlist').innerHTML = ele;
}

// 续费
function submitDatabase() {
    clearTimeout(orderTimeout);
    var data_opt = {
        key: $('.contbox a.active').attr('data-key')
    }
    var modalId = modal.load.show();
    ajax.post('/api?wx.pay', data_opt, function(res) {
        modal.load.hide(modalId);
        if(res.state) {
            var list = res.data;
            var href = list.mweb_url + '&redirect_url=' + encodeURIComponent('http://sg.51hub.cn/pay' + '?order=' + list.out_trade_no)
            window.location.href = href;
        } else {
        		modal.tips(res.error || '异常');
        }
    });
}